package com.jrx.anytxn.common.uitl;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuzb
 * @date 2023年08月10日 16:50
 */
public class MavenUpload {

    public static void main(String[] args) throws Exception {
        String newFilePath = "D:\\mavenUpload\\mavenUpload.bat";
        File newFile = new File(newFilePath);
        if (!newFile.exists()) {
            newFile.createNewFile();
        }
        FileWriter out = new FileWriter(newFile);
        BufferedWriter bw2 = new BufferedWriter(out);
        String str =
                //"D:\\.m2\\repository\\cn\\com\\xib\\ifsp.bio&D:\\.m2\\repository\\cn\\com\\xib\\ifsp.bio-frau";
                "D:\\.m2\\repository\\cn\\com\\xib\\dup.parent\\0.4.4-SNAPSHOT";
        List<String> list = Arrays.asList(str.split("&"));

        //// 本地的仓库地址
        //List<String> list = Arrays.asList(
        //        "D:\\.m2\\repository\\org\\springframework\\cloud\\spring-cloud-cloudfoundry-dependencies\\2.0.0.M3",
        //        "D:\\.m2\\repository\\org\\springframework\\cloud\\spring-cloud-security-dependencies\\2.0.0.M3",
        //        "D:\\.m2\\repository\\org\\springframework\\cloud\\spring-cloud-task-dependencies\\2.0.0.M3"
        //);
        //// 本地的仓库地址
        //List<String> list = Arrays.asList(
        //        "D:\\.m2\\repository\\cn\\com\\xib\\dup"
        //        ,"D:\\.m2\\repository\\cn\\com\\xib\\dup.cache\\0.4.6-SNAPSHOT"
        //        ,"D:\\.m2\\repository\\cn\\com\\xib\\dup.core\\0.4.6-SNAPSHOT"
        //        ,"D:\\.m2\\repository\\cn\\com\\xib\\dup.cos"
        //        ,"D:\\.m2\\repository\\cn\\com\\xib\\dup.instrument"
        //        ,"D:\\.m2\\repository\\cn\\com\\xib\\dup.log"
        //        ,"D:\\.m2\\repository\\cn\\com\\xib\\dup.mask"
        //        ,"D:\\.m2\\repository\\cn\\com\\xib\\dup.parent"
        //);
        for (int i = 0; i < list.size(); i++) {
            String filePath = list.get(i);
            Map<String, String> map = new HashMap<>();
            ProcessBuilder pb = new ProcessBuilder("cmd.exe");
            pb.directory(new File(filePath));
            Process p = pb.start();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
            // 列出目录下所有的文件夹和文件
            bw.write("dir /s/b \n");
            bw.flush();
            bw.close();
            InputStream is = p.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "GBK");
            BufferedReader br = new BufferedReader(isr);
            String line;
            while ((line = br.readLine()) != null) {
                String[] split = line.split("\\\\");
                String key = split[split.length - 1];
                if (key.contains(".")) {
                    key = key.substring(0, key.lastIndexOf("."));
                    boolean endJar = line.endsWith(".jar");
                    boolean endPom = line.endsWith(".pom");
                    if (endJar) {
                        // 获取需要执行的mvn命令
                        String url = setUrl(line, true);
                        bw2.write(url);
                        bw2.newLine();
                        bw2.newLine();
                        map.put(key, line);
                    }
                    if (endPom && !map.containsKey(key)) {
                        String url = setUrl(line, false);
                        bw2.write(url);
                        bw2.newLine();
                        bw2.newLine();
                        map.put(key, line);
                    }
                }
            }
        }
        bw2.write("pause");
        bw2.close();
        Runtime.getRuntime().exec("cmd /c start " + newFilePath);
    }

    public static String setUrl(String data, boolean conJar) {
        boolean snapshot = data.contains("SNAPSHOT");
        String position = "ccs-releases";
        if (snapshot) {
            position = "ccs-snapshots";
        }
        String Dpackaging = "jar";
        String[] split = data.split(",");
        for (int i = 0; i < split.length; i++) {

            String path = split[i];
            String[] item = path.split("\\\\");
            String version = item[item.length - 2];
            String artifactId = item[item.length - 3];
            String groupId = "";
            for (int j = 3; j < item.length - 3; j++) {
                groupId = groupId + item[j] + ".";
            }
            String url =
                    "call mvn deploy:deploy-file " +
                            " -DgroupId=" + groupId.substring(0, groupId.length() - 1) + " " +
                            " -DartifactId=" + artifactId + " " +
                            " -Dversion=" + version + " ";
            if (conJar) {
                    url = url + " -DpomFile=" + path.substring(0, path.length() - 4) + ".pom ";
            } else {
                path = path.replaceAll("jar", "pom");
                Dpackaging = "pom";
            }
            url = url + " -Dpackaging=" + Dpackaging + " -Dfile=" + path +
                    " -Durl=http://ccs:ccs#0106@10.71.96.157:8081/repository/" + position + "/ " +
                    " -DrepositoryId=" + position +
                    // maven配置文件地址
                    " --settings D:\\.m2\\settings.xml";
            return url;
        }
        return "";
    }

}
